package com.app.framework.config;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RedissonConfig
 */
@Configuration
public class RedissonConfig {

    /**
     * Redis地址
     */
    @Value("${spring.data.redis.host}")
    private String host;

    /**
     * Redis端口
     */
    @Value("${spring.data.redis.port}")
    private String port;

    /**
     * Redis密码
     */
    @Value("${spring.data.redis.password}")
    private String password;

    /**
     * Redis库
     */
    @Value("${spring.data.redis.database}")
    private int database;

    @Bean
    public RedissonClient redissonClient() {
        //实例化配置对象
        Config config = new Config();
        //设置Redis服务器的地址和密码
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
        if (StringUtils.isEmpty(password)) {
            config.useSingleServer().setPassword(null);
        } else {
            config.useSingleServer().setPassword(password);
        }
        return Redisson.create(config);
    }

}